// pages/address/address.js
const app=getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
	  switch_off: true,
    receiverName: "",
    mobile: "",
    addressDetail: "",
    postCode: "336600",
    isDisabled: false,
    isComplete: false,
    buttonTitle: "保存",
    region: ['请选择省', '请选择市', '请选择区'],
		biaoqian:[{'value':'家',id:0},{'value':'公司',id:1},{'value':'学校',id:2}],
		biaoqianstutas:false,
		sexarr:[
			{'value':'先生','id':0,'checked':true},
			{'value':'女士','id':1,'checked':false},
		],
		sex:0,
		label:'',
		region: ['请选择省', '请选择市', '请选择区']
	},
	/* 选择标签 */
	biaoqian(e){
		  let id=e.currentTarget.dataset.id;
			let biaoqianstutas=this.data.biaoqianstutas=true;
			this.data.label=e.currentTarget.dataset.value;
			this.setData({
				id:id
			})
	},
	radio:function(e){
		this.data.sex=e.currentTarget.dataset.id;
	  this.setData({
	    value: e.currentTarget.dataset.id
	  })
	},
  bindRegionChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value);
		this.data.region=e.detail.value[0]+e.detail.value[1]+e.detail.value[2];
		//wx.setStorageSync('region',e.detail.value);
		console.log(this.data.region);
    this.setData({
      region: e.detail.value
    })
  },
  formSubmit(e) {
		let region=this.data.region;
		console.log(region);
		let label=this.data.label;
		let sex=this.data.sex;
		let addrInfo={
			'linkMan':e.detail.value.receiverName,
			'phoneNumber':e.detail.value.mobile,
			'deliveryAddress':region,
			'houseNumber':e.detail.value.addressDetail,
			'sex':sex,
			'label':label,
			'frequency':e.detail.value.switch
		}
		console.log(addrInfo);
		let biaoqianstutas=this.data.biaoqianstutas;/* 判断是否选择家标签 */
		wx.setStorageSync('addrInfo',addrInfo)
		//console.log(receiverName,mobile,region_shi,address_total,switch_off)
		if(addrInfo.linkMan==""||addrInfo.phoneNumber==""||addrInfo.deliveryAddress[0]=="请选择省"||addrInfo.houseNumber==""){
				this.data.isComplete=false;
        wx.showModal({
					title:"提示",
					content:"请完善信息",
					showCancel:false
       }); 
    }else if(!/^[1][3,4,5,7,8]\d{9}$/.test(addrInfo.phoneNumber)){
      wx.showModal({
        title:"提示",
        content:"手机号格式不规范",
        showCancel:false
      }); 
    }else if(biaoqianstutas==false){
			wx.showModal({
			  title:"提示",
			  content:"请选择标签",
			  showCancel:false
			});
		}else{
      this.data.isComplete=true;
      //wx.setStorageSync('addrInfo', addrInfo);
      this.updateAddr();
    }
    this.setData({
        isDisabled: false,
        isComplete: this.data.isComplete
        // buttonTitle: "修改"
    });
	
  },
	/* 提交地址 */
  updateAddr(e){
	  let userInfo=wx.getStorageSync('userInfo');
	  let addrInfo=wx.getStorageSync('addrInfo');
	  //console.log(addrInfo)
	  let that = this;
		let sex=this.data.sex;
	  let base_url = app.globalData.baseurl;
	  let _url = base_url + '/address/add';
	  console.log(addrInfo)
		let data={
				'openId':userInfo.openId,
				'userId':userInfo.userId,
				'linkMan':addrInfo.linkMan,
				'sex':addrInfo.sex,
				'phoneNumber':addrInfo.phoneNumber,
				'deliveryAddress':addrInfo.deliveryAddress,
				'houseNumber':addrInfo.houseNumber,
				'label':addrInfo.label,
				'frequency':addrInfo.frequency
		};
		//return false;
		utils.request(userInfo,_url,'post','',res=>{if(res.data.status=200){
				wx.redirectTo({
					url:'../confirmorder/confirmorder'
				})
			}
			
				// for (var i = 0; i < res.data.index_slide.length; i++) {
				//   var item = res.data.data[i];
				//   item.imageurl = item.prex + item.path;
				//   res.data.data[i] = item;
				// }
// 	  			that.setData({
// 	  				address_list:res.data.address_list,
// 	  			})
			
		})
	  this.setData({
      isDisabled:false,
      isComplete:false,
      buttonTitle: "保存"
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let addrInfo=wx.getStorageSync("addrInfo");
    console.log(addrInfo);
    let {receiverName,mobile,addressDetail,postCode}=addrInfo;
    this.setData({
      receiverName:receiverName||this.data.receiverName,
      mobile:mobile||this.data.mobile,
      addressDetail:addressDetail||this.data.addressDetail,
      postCode:postCode||this.data.postCode,
      isDisabled: true,
      isComplete:true,
      buttonTitle: "新增收货地址"
    });
		
		/* for (var i = 1; i <= 2; ++i) {
		  (function (index) {
		    Page[`switch${index}Change`] = function (e) {
		      console.log(`switch${index}发生change事件，携带值为`, e.detail.value)
		      var obj = {}
		      obj[`switch${index}Checked`] = e.detail.value
		      this.setData(obj)
		      obj = {}
		      obj[`switch${index}Style`] = e.detail.value ? '' : 'text-decoration: line-through'
		      this.setData(obj)
		    }
		  })(i)
		} */
		
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})